// Entities

Assault.Entities              = {}
Assault.Entities.List         = {}
Assault.Entities.Spawned      = {}
Assault.Entities.CurrentIndex = 1

// New

function Assault.Entities:New(Class)
	local Table = {}
	
	// Meta table
	
	setmetatable(Table, self)
	
	// Index
	
	self.__index = self
	
	// Class
	
	Table.Class = Class
	
	// Return table
	
	return Table
end

// Entities get by class

function Assault.Entities.EntitiesGetByClass(Class)
	local Table = {}
	
	// Loop
	
	for K, V in pairs(Assault.Entities.Spawned) do
		local Result = V:EntityGetClass()
		
		// Check result
		
		if (Result == Class) then
			local Index = V:EntityGetIndex()
			
			// Add it to the table
			
			Table[Index] = V
		end
	end
	
	// Return table
	
	return Table
end

// Entities get by name

function Assault.Entities.EntitiesGetByName(String)
	local Table = {}
	
	// Loop
	
	for K, V in pairs(Assault.Entities.Spawned) do
		local Result = V:EntityGetName()
		
		// Check result
		
		if (Result == String) then
			local Index = V:EntityGetIndex()
			
			// Add it to the table
			
			Table[Index] = V
		end
	end
	
	// Return table
	
	return Table
end

// Entities get by index

function Assault.Entities.EntitiesGetByIndex(Index)
	return Assault.Entities.Spawned[Index]
end

// Entities hook call

function Assault.Entities.EntitiesHookCall(ID, ...)
	if (type(ID) != "table") then ID = {ID} end
	
	// Entities
	
	for K, V in pairs(Assault.Entities.Spawned) do
		for B, J in pairs(ID) do
			if (V[J]) then
				local Fine, Error = pcall(V[J], V, unpack(arg))
				
				// Check error
				
				if (!Fine) then
					print(Error)
				end
			end
		end
	end
end

// Entities remove all

function Assault.Entities.EntitiesRemoveAll()
	for K, V in pairs(Assault.Entities.Spawned) do
		V:EntityRemove()
	end
end

// Entities quick create

function Assault.Entities.EntitiesQuickCreate(Class, Vector, W, H, Color, Variables)
	local Entity = Assault.Entity:New(Class)
	
	// Position
	
	if (W and H) then Entity:EntitySetSize(W, H) end
	if (Vector) then Entity:EntitySetPosition(Vector) end
	if (Color) then Entity:EntitySetColor(Color) end
	
	// Variables
	
	if (Variables) then
		for K, V in pairs(Variables) do
			Entity:EntitySetVariable(K, V)
		end
	end
	
	// Create
	
	Entity:Create()
	
	// Return entity
	
	return Entity
end

// Create

function Assault.Entities:Create()
	Assault.Entities.List[self.Class] = self
end

// Load entities

local Files = file.FindInLua("Assault/Entities/*.lua")

// Loop

for K, V in pairs(Files) do
	include("Assault/Entities/"..V)
end